<html>
<head><meta charset="utf-8"><title>Detecting blocking functions in async code · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html">Detecting blocking functions in async code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216420473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216420473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216420473">(Nov 12 2020 at 02:19)</a>:</h4>
<p><span class="user-mention" data-user-id="137147">@Didrik Nordstrom</span> and I were talking about this and he opened <a href="https://github.com/rust-lang/wg-async-foundations/issues/19">wg-async-foundations#19</a> which is a great problem summary</p>



<a name="216420543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216420543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216420543">(Nov 12 2020 at 02:21)</a>:</h4>
<p>I think some of this might be relevant to the <code>must_not_await</code> lint RFC</p>



<a name="216420606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216420606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216420606">(Nov 12 2020 at 02:22)</a>:</h4>
<p>For one, because it shares some of the same sub-problems (transitivity, whether to implement with traits or not)</p>



<a name="216420631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216420631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216420631">(Nov 12 2020 at 02:23)</a>:</h4>
<p>And because it raises the question of what the desired end state looks like, if we end up doing this <em>and</em> <code>must_not_await</code></p>



<a name="216420707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216420707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216420707">(Nov 12 2020 at 02:24)</a>:</h4>
<p>My feeling is that we'll want this eventually, for the reasons laid out in the issue, but I'm curious to hear what others think</p>



<a name="216487623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216487623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216487623">(Nov 12 2020 at 15:59)</a>:</h4>
<p>Agreed that some variant of this would indeed be useful</p>



<a name="216492750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216492750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucio Franco <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216492750">(Nov 12 2020 at 16:32)</a>:</h4>
<p>I don't think it makes sense to bake into the type system/compiler the notion of blocking code because its definition can change. For example, writev can block depending on the flag set. How do we encode this? I think the best way to detect blocking is to have the runtime report it or possibly provide hook  ins for preemption (not sure how to actually do this). All seem super invasive.</p>



<a name="216493157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216493157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216493157">(Nov 12 2020 at 16:35)</a>:</h4>
<p>I think it's fine to have it as a lint</p>



<a name="216493808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216493808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucio Franco <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216493808">(Nov 12 2020 at 16:39)</a>:</h4>
<p>left a comment probably better to discuss there</p>



<a name="216946016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216946016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Didrik Nordstrom <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216946016">(Nov 16 2020 at 23:11)</a>:</h4>
<p>Thanks for the feedback so far. Not having a concept of what might block (eventually), i.e. leaving it as an implementation detail, would allow for an implementation to change from non-blocking to blocking, which could break async applications. Not that I know of any obvious risks where this might occur in practice, but it highlights a piece of implicit or manual judgment in the maintenance process. <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="216950997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Detecting%20blocking%20functions%20in%20async%20code/near/216950997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Detecting.20blocking.20functions.20in.20async.20code.html#216950997">(Nov 17 2020 at 00:11)</a>:</h4>
<p>I believe changing a function from async -&gt; blocking can't lead to data races the way it does in other languages, but may still lead to deadlocks since entire threads can be prevented from making progress.</p>
<p>Def agree a lint would be great here. I've seen people try to join threads inside async blocks, and would've loved to have a mechanism to warn them not to do that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>